import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;
import java.awt.*;
import java.awt.geom.Rectangle2D;

public class PptWaterMarkUtil {
    public static void main(String[] args) throws Exception{
        //加载PPT源文档
        Presentation ppt = new Presentation();
        ppt.loadFromFile("C:\\Users\\Xiaoi\\Desktop\\测试用文件夹\\2021-11 WaterMark\\测试用.pptx");

        ISlide slide = null;
        //获取指定幻灯片
        for (int o = 0; o < ppt.getSlides().getCount(); o++) {
            slide = ppt.getSlides().get(o);

            //设置文本水印文本宽和高
            int width= 110;
            int height= 80;

            //起始坐标
            float x = 10;
            float y = 40;
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    //绘制文本，设置文本格式并将其添加到第一张幻灯片
                    Rectangle2D.Double rect = new Rectangle2D.Double(x,y,width, height);
                    IAutoShape shape = slide.getShapes().appendShape(ShapeType.RECTANGLE, rect);
                    shape.getFill().setFillType(FillFormatType.NONE);
                    shape.getShapeStyle().getLineColor().setColor(Color.white);
                    shape.setRotation(-45);
                    shape.getLocking().setSelectionProtection(true);
                    shape.getLine().setFillType(FillFormatType.NONE);
                    shape.getTextFrame().setText("刘肖阳  1412");
                    shape.setShapeArrange(ShapeAlignmentEnum.ShapeArrange.SendToBack);
                    PortionEx textRange = shape.getTextFrame().getTextRange();
                    textRange.getFill().setFillType(FillFormatType.SOLID);
                    textRange.getFill().getSolidColor().setColor(new Color(Integer.parseInt("#C5CBCF".substring(1), 16)));
                    textRange.setFontHeight(20);
                    x += (100 + ppt.getSlideSize().getSize().getWidth()/6);
                }
                x = 30;
                y += (100 + ppt.getSlideSize().getSize().getHeight()/7) ;
            }
        }

        //保存文档
        ppt.saveToFile("C:\\Users\\Xiaoi\\Desktop\\测试用文件夹\\2021-11 WaterMark\\测试用2.pptx", FileFormat.PPTX_2013);
        ppt.dispose();
    }
}